package team.pra.manager.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import team.pra.manager.common.ManagerResult;
import team.pra.manager.common.PageHelper;
import team.pra.manager.mapper.UserMapper;
import team.pra.manager.pojo.User;
import team.pra.manager.service.UserService;

import java.util.List;

@Service
public class UserServiceimpl implements UserService {
    @Autowired(required = false)
    private UserMapper userMapper;
    @Override
    public User loginQuery(User user) {
        return userMapper.login(user);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ManagerResult insertoToUser(User user) {
        return ManagerResult.ok(userMapper.insertToUser(user));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ManagerResult deleteUser(Integer id) {
        return ManagerResult.ok(userMapper.deleteUser(id));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ManagerResult updateUser(User user) {
        return ManagerResult.ok(userMapper.updateUser(user));
    }

    @Override
    public void findUserByCondition(PageHelper<User> pageHelper) {
        List<User> users = userMapper.findUserByCondition(pageHelper);
        Integer filterTotal = userMapper.countFilterUserNumber(pageHelper);
        pageHelper.setRecordsFiltered(filterTotal);
        pageHelper.setRecordsTotal(userMapper.countUserNumber());
        pageHelper.setData(users);
    }
}
